package jh.com.jhappbf.work;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.util.AbDialogUtil;
import com.ab.view.titlebar.AbTitleBar;
import com.ab.view.wheel.AbWheelUtil;
import com.ab.view.wheel.AbWheelView;
import com.blankj.utilcode.util.TimeUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import androidx.core.content.ContextCompat;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import jh.com.jhappbf.R;

/**
 * @author : wwh
 * e-mail  : 953889276@qq.com
 * time    : 2019-6-22 16:41:46
 * desc    :
 */
public class WorkSettingActivity extends AbActivity {

    @BindView(R.id.query_work_setting_start_date_txt)
    TextView startDateTxt;
    @BindView(R.id.query_work_setting_start_date_btn)
    Button startDateBtn;
    @BindView(R.id.query_work_setting_start_time_txt)
    TextView startTimeTxt;
    @BindView(R.id.query_work_setting_start_time_btn)
    Button startTimeBtn;
    @BindView(R.id.query_work_setting_end_date_txt)
    TextView endDateTxt;
    @BindView(R.id.query_work_setting_end_date_btn)
    Button endDateBtn;
    @BindView(R.id.query_work_setting_end_time_txt)
    TextView endTimeTxt;
    @BindView(R.id.query_work_setting_end_time_btn)
    Button endTimeBtn;
    private View mWheelDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.activity_work_setting);
        ButterKnife.bind(this);

        AbTitleBar mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText(R.string.query_weight_setting);
        mAbTitleBar.setLogo(R.drawable.button_selector_back);
        mAbTitleBar.setTitleBarBackgroundColor(ContextCompat.getColor(this, R.color.orange));
        mAbTitleBar.setTitleTextMargin(10, 0, 0, 0);
        mAbTitleBar.setLogoLine(R.mipmap.line);
        mAbTitleBar.setLogoOnClickListener(v -> saveSettings());
        initTime();
    }

    private void initTime() {
        String nowDate = TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()));
        startDateTxt.setText(nowDate);
        startTimeTxt.setText("00:00:00");
        endDateTxt.setText(nowDate);
        endTimeTxt.setText("23:59:59");
    }

    private void saveSettings() {
        Intent intent = new Intent();
        intent.putExtra("startTime", startTimeTxt.getText().toString().trim());
        intent.putExtra("startDate", startDateTxt.getText().toString().trim());
        intent.putExtra("endTime", endTimeTxt.getText().toString().trim());
        intent.putExtra("endDate", endDateTxt.getText().toString().trim());
        setResult(1, intent);
        finish();
    }

    @OnClick({R.id.query_work_setting_start_date_txt, R.id.query_work_setting_start_date_btn, R.id.query_work_setting_start_time_txt, R.id.query_work_setting_start_time_btn, R.id.query_work_setting_end_date_txt, R.id.query_work_setting_end_date_btn, R.id.query_work_setting_end_time_txt, R.id.query_work_setting_end_time_btn})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.query_work_setting_start_date_txt:
                initWheelThree(v, true);
                break;
            case R.id.query_work_setting_start_date_btn:
                startDateTxt.setText("");
                break;
            case R.id.query_work_setting_start_time_txt:
                initWheelThreeTime(v);
                break;
            case R.id.query_work_setting_start_time_btn:
                startTimeTxt.setText("");
                break;
            case R.id.query_work_setting_end_date_txt:
                initWheelThree(v, false);
                break;
            case R.id.query_work_setting_end_date_btn:
                endDateTxt.setText("");
                break;
            case R.id.query_work_setting_end_time_txt:
                initWheelThreeTime(v);
                break;
            case R.id.query_work_setting_end_time_btn:
                endTimeTxt.setText("");
                break;
            default:
        }
    }

    /**
     * 设置日期
     */
    private void initWheelThree(View txt, boolean isStart) {
        mWheelDate = mInflater.inflate(R.layout.wheel_three_activity, null);
        initWheelDate(mWheelDate, (TextView) txt, "yyyy-MM-dd", isStart);
        AbDialogUtil.showDialog(mWheelDate, Gravity.BOTTOM);
    }

    /**
     * 设置时间
     */
    private void initWheelThreeTime(View txtTime) {
        mWheelDate = mInflater.inflate(R.layout.wheel_three_activity, null);
        initWheelTime(mWheelDate, (TextView) txtTime);
        AbDialogUtil.showDialog(mWheelDate, Gravity.BOTTOM);
    }

    public void initWheelDate(View mDateView, TextView mText, String flag, boolean isStart) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DATE);
        if (isStart) {
            day = day - 1;
        }
        final AbWheelView mWheelViewY = mDateView.findViewById(R.id.wheelView1);
        final AbWheelView mWheelViewM = mDateView.findViewById(R.id.wheelView2);
        final AbWheelView mWheelViewD = mDateView.findViewById(R.id.wheelView3);
        if ("yyyy-MM".equals(flag)) {
            mWheelViewD.setVisibility(View.GONE);
        } else if ("yyyy".equals(flag)) {
            mWheelViewD.setVisibility(View.GONE);
            mWheelViewM.setVisibility(View.GONE);
        }
        Button okBtn = mDateView.findViewById(R.id.okBtn);
        Button cancelBtn = mDateView.findViewById(R.id.cancelBtn);
        mWheelViewY.setCenterSelectDrawable(ContextCompat.getDrawable(this, R.drawable.wheel_select));
        mWheelViewM.setCenterSelectDrawable(ContextCompat.getDrawable(this, R.drawable.wheel_select));
        mWheelViewD.setCenterSelectDrawable(ContextCompat.getDrawable(this, R.drawable.wheel_select));
        AbWheelUtil.initWheelDatePicker(this, mText, mWheelViewY, mWheelViewM,
                mWheelViewD, okBtn, cancelBtn, year, month, day, 2000, 30,
                true, flag);
    }

    /**
     * 初始化时间
     *
     * @param mDateView
     * @param mText
     * @param
     */
    public void initWheelTime(View mDateView, TextView mText) {
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE) + 1;
        final AbWheelView mWheelViewH = mDateView.findViewById(R.id.wheelView1);
        mWheelViewH.setVisibility(View.GONE);
        final AbWheelView mWheelViewM = mDateView.findViewById(R.id.wheelView2);
        final AbWheelView mWheelViewS = mDateView.findViewById(R.id.wheelView3);

        Button okBtn = mDateView.findViewById(R.id.okBtn);
        Button cancelBtn = mDateView.findViewById(R.id.cancelBtn);
        mWheelViewM.setCenterSelectDrawable(ContextCompat.getDrawable(this, R.drawable.wheel_select));
        mWheelViewS.setCenterSelectDrawable(ContextCompat.getDrawable(this, R.drawable.wheel_select));

        AbWheelUtil.initWheelTimePicker2(this, mText, mWheelViewM, mWheelViewS,
                okBtn, cancelBtn, hour, minute, true);
    }


    @Override
    public void onBackPressed() {
        saveSettings();
    }
}
